package com.aqiang.day0528_shop.main.view.activity

import android.graphics.Canvas
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.aqiang.day0528_shop.R
import com.aqiang.day0528_shop.main.adapter.AddressAdapter
import com.aqiang.day0528_shop.main.contract.GoodsTypeContract
import com.aqiang.day0528_shop.main.entity.UserAdress
import com.aqiang.day0528_shop.sql.UserInfo
import com.aqiang.lib_core.mvp.BaseActivity
import com.chad.library.adapter.base.listener.OnItemSwipeListener
import kotlinx.android.synthetic.main.activity_receipt.*
import org.jetbrains.anko.startActivity
import android.support.v7.widget.helper.ItemTouchHelper
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback



class ReceiptActivity : BaseActivity<GoodsTypeContract.GoodsTypePresenter>() {
    private var userInfo: UserInfo? = null
    private var data:MutableList<UserAdress>? = null
    private var addressAdapter:AddressAdapter? = null
    override fun bindLatout(): Int {
        return R.layout.activity_receipt
    }

    override fun createPresenter() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        rv_act_receipt.layoutManager = LinearLayoutManager(this)

    }

    override fun initData() {
        data = mutableListOf()

    }

    override fun initEvent() {
        bt_act_receipt_add.setOnClickListener {
            startActivity<AddActivity>()
        }

    }

    override fun onResume() {
        super.onResume()
        userInfo = UserInfo.getInstance(this)
        userInfo!!.use {
            val cursor = query(UserInfo.sqlName, null, null, null, null, null, null)
            if(cursor != null){
                while (cursor.moveToNext()){
                    val name = cursor.getString(cursor.getColumnIndex("name"))
                    val tel = cursor.getString(cursor.getColumnIndex("tel"))
                    val area = cursor.getString(cursor.getColumnIndex("area"))
                    val address = cursor.getString(cursor.getColumnIndex("address"))
                    val userAdress = UserAdress(name, tel, area, address)
                    if(!data!!.contains(userAdress)){
                        data!!.add(userAdress)
                    }
                }
            }
        }
        if(addressAdapter == null){
            addressAdapter = AddressAdapter(com.aqiang.day0528_shop.R.layout.item_re,data!!)
            rv_act_receipt.adapter = addressAdapter
        }else{
            addressAdapter!!.notifyDataSetChanged()
        }
        addressAdapter?.setOnItemClickListener { adapter, view, position ->
            startActivity<AddActivity>()
        }
        val itemDragAndSwipeCallback = ItemDragAndSwipeCallback(addressAdapter)
        val itemTouchHelper = ItemTouchHelper(itemDragAndSwipeCallback)
        itemTouchHelper.attachToRecyclerView(rv_act_receipt)
        addressAdapter!!.enableSwipeItem();
        addressAdapter!!.setOnItemSwipeListener(object :OnItemSwipeListener{
            override fun clearView(viewHolder: RecyclerView.ViewHolder?, pos: Int) {
//                data!!.removeAt(pos)
//                addressAdapter!!.notifyDataSetChanged()
            }

            override fun onItemSwiped(viewHolder: RecyclerView.ViewHolder?, pos: Int) {

            }

            override fun onItemSwipeStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) {

            }

            override fun onItemSwipeMoving(
                canvas: Canvas?,
                viewHolder: RecyclerView.ViewHolder?,
                dX: Float,
                dY: Float,
                isCurrentlyActive: Boolean
            ) {

            }
        });
    }
}
